import { Body, Controller, Inject, Post } from '@midwayjs/core';
import { ShCustomer, ShCustomerInsertDto } from '../entity/ShCustomer.entity';
import { Repository } from 'typeorm';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { AccountNumError } from '../filter/Error.filter';
import { BillCodeGenerator } from '../service/IdTool.entity';
@Controller('/ShCustomer')
export class ShCustomerController {
  @InjectEntityModel(ShCustomer)
  repo: Repository<ShCustomer>;
  @Inject()
  cuCode: BillCodeGenerator;
  @Post('/insert')
  async insert(@Body() dto: ShCustomerInsertDto) {
    const cu = new ShCustomer();
    const cur = await this.repo.findOneBy({ cuAccountNum: dto.cuAccountNum });
    if (cur) {
      throw new AccountNumError('您要注册的账号已存在');
    }
    cu.cuAccountNum = dto.cuAccountNum;
    cu.cuId = await this.cuCode.nextCode('Cu');
    cu.cuNikeName = dto.cuNikeName;
    cu.cuPassword = dto.cuPassword;
    cu.cuPhone = dto.cuPhone;
    cu.cuState = dto.cuState;
    return await this.repo.save(cu);
  }
}
